package com.air.ota.controller.imp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import com.air.common.controller.imp.BaseControllerImp;
import com.air.ota.controller.ITriggerTestController;
import com.air.ota.mo.OutboundMsg;
import com.air.ota.service.IOutboundMsgService;
import com.air.ota.service.IPkgSequenceService;
import com.air.ota.util.AppConstant;
import com.air.ota.util.HexStringUtil;
import com.air.ota.vo.UTKDepositSyncTriggerDownlinkVO;
import com.air.ota.vo.UTKDepositTriggerDownlinkVO;

public class TriggerTestControllerImp extends BaseControllerImp implements
		ITriggerTestController {
	
	@Autowired
	IOutboundMsgService outboundMsgService;
	@Autowired
	IPkgSequenceService pkgSequenceService;
	
	@Override
	public ModelAndView sendDepositSyncTrigger(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		UTKDepositSyncTriggerDownlinkVO downlinkVO = new UTKDepositSyncTriggerDownlinkVO(
				HexStringUtil.hexStringToBytes("0011075725448937"),
				pkgSequenceService.getNextSequence("1308126386015"),
				HexStringUtil.hexStringToBytes("51800000D1CF4911"),
				HexStringUtil.hexStringToBytes("0000000000000001")
			);
		byte[] responsePkg=downlinkVO.buildPkg();
		
		OutboundMsg outboundMsg = new OutboundMsg();
		outboundMsg.setContent(HexStringUtil.bytesToHexString(responsePkg));
		outboundMsg.setDest(0);
		outboundMsg.setPhoneNumber("1308126386015");
		outboundMsg.setSendDate(System.currentTimeMillis());
		outboundMsg.setTransId(-1);
		outboundMsg.setNeedRetry(1);
		outboundMsg.setMsgType("DEPOSIT_SYNC_TRIGGER_TEST");
		outboundMsgService.insert(outboundMsg);
		
		return null;
	}

	@Override
	public ModelAndView sendDepositTrigger(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		UTKDepositTriggerDownlinkVO downlinkVO = new UTKDepositTriggerDownlinkVO(
				HexStringUtil.hexStringToBytes("0011075725448937"),
				pkgSequenceService.getNextSequence("1308126386015"),
				HexStringUtil.hexStringToBytes("51800000D1CF4911"),
				HexStringUtil.hexStringToBytes("00000100"),
				HexStringUtil.hexStringToBytes("0000000000000002")
			);
		byte[] responsePkg=downlinkVO.buildPkg();
		
		OutboundMsg outboundMsg = new OutboundMsg();
		outboundMsg.setContent(HexStringUtil.bytesToHexString(responsePkg));
		outboundMsg.setDest(0);
		outboundMsg.setPhoneNumber("1308126386015");
		outboundMsg.setSendDate(System.currentTimeMillis());
		outboundMsg.setTransId(-1);
		outboundMsg.setNeedRetry(1);
		outboundMsg.setMsgType("DEPOSIT_TRIGGER_TEST");
		outboundMsgService.insert(outboundMsg);
		
		return null;
	}

}
